<?php
namespace app\index\controller;
use app\common\controller\UserCm;
use think\Db;
use think\Request;
use think\Validate;
use app\index\model\Pass;

class Regist extends BaseController {

  public function index(){
  	return $this->fetch();
  }
  
  public function regist(Request $request){
    $user = new Pass;
// 自定义验证
    $data['username'] = $request->param('username');
    $data['password'] = $request->param('password');
    $data['email'] = $request->param('email');
    $rule=[
        ['username', 'require|max:10','名字不能为空|名字不能超过十位'],
        ['email', 'require|email','邮箱不能为空|邮箱格式不正确'],
        ['password','require','密码不能为空']
      ];
   $validate = new Validate($rule);
	$result   = $validate->check($data);
	if(!$result){
	    return $this->error($validate->getError());
	}else{
	    $user1 = Db::name('user')->where('email',$data['email'])->find();
	    $user2 = Db::name('user')->where('username',$data['username'])->find();
	    if(!empty($user1))
        {
            $data['status'] = 1;
            $data['msg'] = '邮箱已经被注册，请换一个吧！';
            return $data;
        }
	    if(!empty($user2))
        {
            $data['status'] = 1;
            $data['msg'] = '用户名存在，请重新输入！';
            return $data;
        }
        $data['add_time'] = date('Y-m-d H:i:s');
        $data['req_ip'] = $this->get_ip();;
        $data['sid'] = 'logout';

        $result = $user->insert($data);
		if ($result) {

            $data['status'] = 0;
            $data['msg'] = '注册成功';
            return $data;
//	      return $this->success('注册成功','/index');
	    } else {
	      return $this->error('注册失败');
	    }
	}

  }

    /**
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @author LeeBW
     * @date 2020-12-29 19:46
     */
    public function ChangePassword(Request $request)
  {
      //参数
      $username = $this->request->param('user');
      $password = $request->param('old_pwd');
      $new_password = $request->param('new_pwd');
      $new_passworda = $request->param('new_pwd_a');

      //判断两次密码是否一致
      if ($new_password != $new_passworda) {
          $data['status'] = 1;
          $data['msg'] = '两次密码不一致';
          return $data;
      }

      $obj_mgr = new UserCm();
      if ($obj_mgr->ChangePassword($username, $password, $new_password)) {
          $data['status'] = 0;
          $data['msg'] = '操作成功';
          return $data;
      } else {
          $data['status'] = 1;
          $data['msg'] = '旧密码不正确';
          return $data;
      }
  }

}